package cn.elricyu.tt.bean;

import org.apache.commons.lang.StringUtils;

import cn.elricyu.tt.util.Base64Util;
import cn.elricyu.tt.util.SystemUtil;

public class KeyBean {
    private static String SEPERATOR = ",";

    private String uuid;

    private String appName;

    private String site;

    private int count;

    private String userId;

    private String masterId;

    private long startTime;

    private long endTime;

    private long now;

    public KeyBean(String snCode) {
        if (!StringUtils.isBlank(snCode)) {
            String[] snArray = snCode.split(SEPERATOR);
            if (snArray != null && snArray.length == 9) {
                String order = snArray[8].trim();
                int[] orderArray = new int[order.length()];
                for (int i = 0; i < order.length(); i++) {
                    int pos = order.charAt(i) - '0';
                    orderArray[i] = pos;
                }
                uuid = snArray[orderArray[0]];
                appName = snArray[orderArray[1]];
                site = snArray[orderArray[2]];
                userId = Base64Decode(snArray[orderArray[3]]);
                masterId = Base64Decode(snArray[orderArray[4]]);
                try {
                    count = Integer.parseInt(snArray[orderArray[5]]);
                } catch (Exception e) {
                }
                try {
                    startTime = Long.parseLong(snArray[orderArray[6]]);
                } catch (Exception e) {
                }
                try {
                    endTime = Long.parseLong(snArray[orderArray[7]]);
                } catch (Exception e) {
                }
                now = SystemUtil.getWebTime();
            }
        }
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getSite() {
        return site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getMasterId() {
        return masterId;
    }

    public void setMasterId(String masterId) {
        this.masterId = masterId;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getEndTime() {
        return endTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    public long getNow() {
        long current = System.currentTimeMillis();
        if (current > now) {
            return current;
        }
        return now;
    }

    public void setNow(long now) {
        this.now = now;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    private String Base64Decode(String str) {
        try {
            return new String(Base64Util.decode(str.getBytes("utf-8")), "utf-8");
        } catch (Exception e) {
            return str;
        }
    }

    public boolean simpleValidate() {
        if (now > startTime && now <= endTime) {
            return true;
        }
        return false;
    }
}
